package ru.compscicenter.databases.zaikina.common.commands;

import org.json.simple.JSONObject;
import org.json.simple.JSONValue;
import ru.compscicenter.databases.zaikina.common.commands.bodies.DocumentsBody;
import ru.compscicenter.databases.zaikina.common.commands.bodies.FullDocumentsBody;
import ru.compscicenter.databases.zaikina.common.model.DocumentWithBody;

import java.util.ArrayList;

public class FullDocumentsCommand extends SysCommand<ArrayList<DocumentWithBody>, FullDocumentsBody> {
    public FullDocumentsCommand(Integer type, FullDocumentsBody body, Integer param) {
        super(type, body, param);
    }

    public FullDocumentsCommand(Integer type, ArrayList<DocumentWithBody> body, Integer param) {
        super(type, body, param);
    }

    public static FullDocumentsCommand parse(String jsonStr) {
        JSONObject obj = (JSONObject) JSONValue.parse(jsonStr);
        if (SysCommandType.isFullDocumentsCommand(SysCommand.parseType(obj))) {
            Integer type = Integer.parseInt(obj.get("type").toString());
            Integer param = Integer.parseInt(obj.get("param").toString());
            FullDocumentsBody body = new FullDocumentsBody();
            body.getFromJsonString(obj.get("body").toString());
            return new FullDocumentsCommand(type, body, param);
        }
        return null;
    }

}
